﻿Imports System.Xml

Partial Class icon
    Inherits System.Web.UI.Page

    Dim error1 As String = "Īsinājumikonas nosaukums pārsniedz maksimālo garumu"
    Dim error2 As String = "Īsinājumikonas nosaukums satur neatļautus simbolus"
    Dim error3 As String = "Īsinājumikonas nosaukums nav ievadīts"
    Dim error4 As String = "Īsinājumikonas bilde nav pieejama"

    '-----------------------------------------------------------
    'Sistēmas īsinājumikonu saraksta pārvaldības funkcijas class
    'strXMLFile - The path and file name of the XML file.
    'strXSLFilee - The path and file name of the XSL file.
    '----------------------------------------------------------- 

    Function checkData(ByVal iconName As String, ByVal IconImage As String) As Boolean


        Dim pattern As String = "^[a-zA-Z0-9]+$"
        Dim reg As New Regex(pattern)
        Dim err As String = ""

        If reg.IsMatch(iconName) = False Then err = error2 & "\n"
        If iconName.Length > 30 Then err = err & error1 & "\n"
        If IconImage = "" Then err = err & error4 & "\n"
        If iconName = "" Then err = err & error3 & "\n"

        If err = "" Then Return True

        Response.Write(err)
        Return False

    End Function

    Dim eMessage1 As String = "Neizdevās pievienot īsinājumikonas informāciju"
    Dim eMessage2 As String = "Neizdevās izmainīt īsinājumikonas informāciju"
    Dim eMessage3 As String = "Neizdevās dzēst īsinājumikonas informāciju"
    Dim eMessage4 As String = "Neizdevās saglabāt rezultātus XML datnē"
    Dim eMessage5 As String = "Neizdevās atvērt XML datni rediģēšanai"

    '-----------------------------------------------------------
    'Sistēmas īsinājumikonu saraksta pārvaldības funkcijas class
    'strXMLFile - The path and file name of the XML file.
    'strXSLFilee - The path and file name of the XSL file.
    '----------------------------------------------------------- 

    Sub iconXmlAction()

        'Sistēmas īsinājumikonu saraksta pārvaldības funkcijas ievaddati

        Dim iconName As String = Request.QueryString("name")
        Dim iconFile As String = Request.QueryString("file")

        Dim iconOrder As String = Request.QueryString("order")
        Dim iconImage As String = Request.QueryString("image")

        Dim iconAction As String = Request.QueryString("action")
        Dim iconAccess As String = Request.QueryString("access")

        If checkData(iconName, iconImage) = True Then

            If iconAction <> "" Then

                Dim xmlDoc As New XmlDocument()

                Try 'atvērt XML datni rediģēšanai
                    xmlDoc.Load(Server.MapPath("xml_icons.xml"))
                    Dim node As XmlNode = xmlDoc.DocumentElement


                    'Sistēmas īsinājumikonas pievienošana
                    If iconAction = "add" Then

                        Dim item As XmlElement = xmlDoc.CreateElement("item")

                        item.SetAttribute("name", iconName)
                        item.SetAttribute("file", iconFile)

                        item.SetAttribute("image", iconImage)
                        item.SetAttribute("level", iconAccess)

                        Try 'pievienot īsinājumikonas informāciju
                            node.InsertBefore(item, node.ChildNodes(iconOrder))
                        Catch exc As Exception

                            Response.Write(eMessage1)
                            Response.Write(exc.ToString)

                        End Try

                    End If

                    'Sistēmas īsinājumikonas dzēšana
                    If iconAction = "delete" Then
                        If iconOrder <> "" Then

                            Dim item As XmlElement = node.ChildNodes(iconOrder)

                            Try 'dzēst īsinājumikonas informāciju
                                node.RemoveChild(item)
                            Catch exc As Exception

                                Response.Write(eMessage3)
                                Response.Write(exc.ToString)

                            End Try

                        End If
                    End If

                    'Sistēmas īsinājumikonas rediģēšana
                    If iconAction = "edit" Then
                        If iconOrder <> "" Then

                            Dim item As XmlElement = node.ChildNodes(iconOrder)

                            item.SetAttribute("name", iconName)
                            item.SetAttribute("file", iconFile)

                            item.SetAttribute("image", iconImage)
                            item.SetAttribute("level", iconAccess)

                            Try 'izmainīt īsinājumikonas informāciju.
                                node.AppendChild(item)
                            Catch exc As Exception

                                Response.Write(eMessage2)
                                Response.Write(exc.ToString)

                            End Try

                        End If
                    End If


                    Try 'saglabāt rezultātus XML datnē
                        xmlDoc.Save(Server.MapPath("xml_icons.xml"))
                    Catch exc As Exception

                        Response.Write(eMessage4)
                        Response.Write(exc.ToString)

                    End Try

                Catch exc As Exception

                    Response.Write(eMessage5)
                    Response.Write(exc.ToString)

                End Try

            End If
        End If

    End Sub

End Class
